package com.dd.common.strategy;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dd.common.strategy.FileStrategy;
import com.dd.config.MinioConfig;
import com.dd.pojo.query.UserFileQuery;
import com.dd.pojo.entry.UserFoldFile;
import com.dd.mapper.UserFoldFileMapper;
import com.dd.common.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 检查当前用户是否已经上传文件
 * 如果已经未上传 将地址转存到数据库
 */
@Component
@RequiredArgsConstructor
public class FileStrategyUserUpload implements FileStrategy {


    private final UserFoldFileMapper userFoldFileMapper;

    private final MinioConfig minioConfig;
    @Override
    public boolean checkFileUpload(UserFileQuery checks) {

        List<UserFoldFile> userFoldFiles = userFoldFileMapper.selectList(new LambdaQueryWrapper<UserFoldFile>().eq(UserFoldFile::getFileId, checks.getFileMd5()));
        if(CollectionUtils.isEmpty(userFoldFiles)){
            //重新组装文件访问路径
            StringBuilder filePath = new StringBuilder().append(minioConfig.getEndpoint())
                    .append("/")
                    .append(minioConfig.getBucketName())
                    .append(checks.getFileMd5())
                    .append(checks.getFileName().substring(checks.getFileName().lastIndexOf(".")));

            UserFoldFile file = UserFoldFile.builder().userId(checks.getUserId())
                            .filePath(filePath.toString())
                            .fileId(checks.getFileMd5())
                            .fileUploadTime(LocalDateTime.now())
                            .folderId(checks.getFolderId())
                            .fileName(checks.getFileName())
                            .fileType(FileUtil.getFileType(checks.getFileName()
                            .substring(checks.getFileName().lastIndexOf(".") + 1))).build();

            userFoldFileMapper.insert(file);


        }

        //判断用户是否已经上传了文件
        boolean exit = userFoldFiles.stream().anyMatch(fi -> {
            return ((fi.getFileName().equals(checks.getFileName())) &&
                    (fi.getFileId().equals(checks.getFileMd5())) &&
                    (fi.getFolderId().equals(checks.getFolderId())));
        });

        if(!exit){
            UserFoldFile file = UserFoldFile.builder().userId(checks.getUserId())
                            .filePath(userFoldFiles.get(0).getFilePath())
                            .fileUploadTime(LocalDateTime.now())
                            .folderId(checks.getFolderId())
                            .fileName(checks.getFileName())
                            .fileId(checks.getFileMd5())
                            .fileType(FileUtil.getFileType(checks.getFileName()
                            .substring(checks.getFileName().lastIndexOf(".") + 1))).build();
            userFoldFileMapper.insert(file);

        }
        return true;
    }
}
